#include "XMLTextIterator.h"
#include "transcoder.h"

using namespace MCCommon;

XMLTextIterator::XMLTextIterator
(
    xercesc::DOMNodeIterator* iter,
    xercesc::DOMNodeFilter* filter /*= 0*/
)
:   fIter(iter),
    fCurNode(0),
    fFilter(filter)
{
    fAccCount.Increase();
}

XMLTextIterator::XMLTextIterator
(
    const XMLTextIterator& eleIter
)
{
    if (fAccCount.GetCount() == 1) 
    {
        release();
    }
    fAccCount.Decrease();
    fIter = eleIter.fIter;
    fCurNode = eleIter.fCurNode;
    fFilter = eleIter.fFilter;
    fAccCount = eleIter.fAccCount;	
    fAccCount.Increase();
}

XMLTextIterator& 
XMLTextIterator::operator = 
(
    const XMLTextIterator& eleIter
)
{
    XMLTextIterator::XMLTextIterator(eleIter);    
    return *this;
}

XMLTextIterator::~XMLTextIterator()
{
    if (fAccCount.GetCount() == 1) 
    {
        release();		
    }
    fAccCount.Decrease();
}

bool 
XMLTextIterator::HasMore()
{
    if(fIter == 0)
        return false;

    fCurNode = fIter->nextNode();
    if(fCurNode == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

std::string 
XMLTextIterator::Next()
{
    std::string text;
    xercesc::DOMText *pDomTxt = 0;
    if( fCurNode != 0 && fCurNode->getNodeType() == xercesc::DOMNode::TEXT_NODE)
    {
        pDomTxt = (xercesc::DOMText*)fCurNode; 
        text  = A(pDomTxt->getData()); 
    }

    return text;    
}

void XMLTextIterator::release()
{
    if(fIter != 0)
    {
        fIter->release();
    }

    if(fFilter != 0)
    {
        delete fFilter;
    }
}
